package com.example.proxy.dynamic.myjdk;

import com.example.proxy.dynamic.myjdk.core.MyClassLoader;
import com.example.proxy.dynamic.myjdk.core.MyInvocationHandler;
import com.example.proxy.dynamic.myjdk.core.MyProxy;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ManProxy implements MyInvocationHandler {

    private Object object;

    public ManProxy(Object object) {
        this.object = object;
    }


    public Object getInstance() {
        return MyProxy.newProxyInstance(new MyClassLoader(),
                this.object.getClass().getInterfaces(),
                this);
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        if (args.length > 0) {
            System.out.println("params");
            for (Object arg : args) {
                System.out.print(arg + "\t");
            }
        }
        System.out.println("\nbefore");
        Object obj = method.invoke(this.object, args);
        System.out.println("after");
        return obj;
    }
}
